<template>
    <div>
        <ListData :list="list" :loading="loading" @setLoading="setLoading" @setFinished="setFinished"
            :finished="finished" @getDemandList="getDemandList" :listCheck="listCheck" :demandState="1"
            :RYD_UserId="RYD_UserId"></ListData>
    </div>
</template>

<script setup>

import { inject, nextTick, onMounted, ref, watch } from 'vue';
import ListData from '../../components/ListData.vue'

import axios from '@/utils/axios';

// eslint-disable-next-line no-unused-vars
import router from '@/router';

let nav = inject('nav');
nav.showLeft = true;
nav.textLeft = '返回';
nav.title = 'ITSM 需求报表';
nav.showRight = true;
nav.visible = false;

let loading = ref(true);
let finished = ref(false);
let RYD_UserId = inject('RYD_UserId');
let RYD_Company = ref();

let list = ref({
    demandList: []
});

let search = inject('search');
watch(search, () => {
    M_DemandList.value.RYD_DemandId = search.RYD_DemandId == '' ? 0 : search.RYD_DemandId;
    M_DemandList.value.RYD_UserName = search.RYD_UserName;
    list.value.demandList = [];
    M_DemandList.value.Page = 1;
    finished.value = false;
    getDemandList();
    /* 深层级变更状态所触发的回调 */
})
// eslint-disable-next-line no-unused-vars
let activeState = ref(['1', '2', '3', '4', '5']);

let M_DemandList = ref({
    RYD_UserId: null,
    RYD_State: activeState.value,
    RYD_Company: RYD_Company,
    Sum: -1,
    Page: 1,
    Size: 20,
    DemandList: [],
    RYD_DemandId: 0,
    RYD_UserName: "",
    StartDateTime: new Date('2021-01-01'),
    EndDateTime: new Date('2090-01-01'),
    SortField: " RYD_DemandList.RYD_CreateTime desc ",
    SortFun: "",
});

let companyList = ref();
let listCheck = ref(false);

let getDemandList = () => {
    loading.value = true;
    axios({
        method: 'post',
        url: '/api/DemandConfirmation',
        data: {
            ...M_DemandList.value
        }
    }).then(listres => {
        list.value.demandList.push(...listres.demandList);
        loading.value = false;
        if (listres.demandList.length < 1) {
            finished.value = true;
        }
        // finished.value = true;
    })
    M_DemandList.value.Page += 1;
}

let setLoading = (value) => {
    loading.value = value;
}


let setFinished = (value) => {
    finished.value = value;
}

let initData = () => {
    Promise.all(
        [
            axios({
                method: 'get',
                url: '/api/GetCompany?RYD_UserId=' + RYD_UserId
            }).then(res => {
                RYD_Company.value = res;
                listCheck.value = true;
                getDemandList();
            }),
            axios({
                method: 'get',
                url: '/api/SetCompany'
            }).then(res => {
                companyList.value = res;
            }),
        ]
    ).then(() => {
    })

}

onMounted(() => {
    nextTick(function () {
        // 仅在整个视图都被渲染之后才会运行的代码
        initData();
    })
})


// eslint-disable-next-line no-unused-vars
let onTabClick = (event) => {
    if (event.name != activeState.value[0]) {
        activeState.value[0] = event.name;
        finished.value = false;
        list.value.demandList = [];
        M_DemandList.value.Page = 1;
    }
}
</script>

<style scoped>
</style>